package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.AddAddressBeen;
import cn.xfdzx.android.apps.shop.bean.AddressListBeen;
import cn.xfdzx.android.apps.shop.bean.DeleteAddressBeen;
import cn.xfdzx.android.apps.shop.bean.LogisticsBeen;
import cn.xfdzx.android.apps.shop.bean.NewOrderBeen;
import cn.xfdzx.android.apps.shop.contract.AddressListContract;
import cn.xfdzx.android.apps.shop.contract.ConfirmRetailContract;
import cn.xfdzx.android.apps.shop.model.AddressListModel;
import cn.xfdzx.android.apps.shop.model.ConfirmRetailModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class ConfirmRetailPresenter extends BasePresenter<ConfirmRetailContract.View> implements ConfirmRetailContract.Presenter {

    private ConfirmRetailContract.Model model;

    public ConfirmRetailPresenter() {
        model = new ConfirmRetailModel();
    }


    @Override
    public void delectAddress(String id,int position) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.delectAddress(id)
                .compose(RxScheduler.<DeleteAddressBeen>Flo_io_main())
                .as(mView.<DeleteAddressBeen>bindAutoDispose())
                .subscribe(new Consumer<DeleteAddressBeen>() {
                    @Override
                    public void accept(DeleteAddressBeen bean) throws Exception {
                        mView.onDelectSuccess(bean,position);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void initAddressData(String page, String limit) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.initAddressData(page, limit).compose(RxScheduler.<AddressListBeen>Flo_io_main())
                .as(mView.<AddressListBeen>bindAutoDispose()).subscribe(new Consumer<AddressListBeen>() {
            @Override
            public void accept(AddressListBeen baseObjectBean) throws Exception {
                mView.onInitAddressSuccess(baseObjectBean);
                mView.hideLoading();
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
                mView.hideLoading();
            }
        });
    }

    @Override
    public void logistics(String address_id, String orderKey) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        model.logistics(address_id, orderKey).compose(RxScheduler.<LogisticsBeen>Flo_io_main())
                .as(mView.<LogisticsBeen>bindAutoDispose()).subscribe(new Consumer<LogisticsBeen>() {
            @Override
            public void accept(LogisticsBeen baseObjectBean) throws Exception {
                mView.onLogisticsSuccess(baseObjectBean);

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);

            }
        });
    }

    @Override
    public void defaultAddress(String id, int position) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.defultAddress(id)
                .compose(RxScheduler.<DeleteAddressBeen>Flo_io_main())
                .as(mView.<DeleteAddressBeen>bindAutoDispose())
                .subscribe(new Consumer<DeleteAddressBeen>() {
                    @Override
                    public void accept(DeleteAddressBeen bean) throws Exception {
                        mView.onDefaultSuccess(bean,position);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void confirmRetail(String orderKey, String couponId, String marks, String payType, String addressId, String leftName, String rightName, String pick_up_time, String reserved_mobile_phone, String delivery_method, String logistics_type,String order_express_type) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.confirmRetail( orderKey,  couponId,  marks,  payType,  addressId,  leftName,  rightName,  pick_up_time,  reserved_mobile_phone,  delivery_method,logistics_type,order_express_type) .compose(RxScheduler.<NewOrderBeen>Flo_io_main())
                .as(mView.<NewOrderBeen>bindAutoDispose())
                .subscribe(new Consumer<NewOrderBeen>() {
                    @Override
                    public void accept(NewOrderBeen bean) throws Exception {
                        mView.onConfirmOrderSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }



}
